/**
* Casey Burkhardt
* CSC 2035 - Algorithms and Data Structures III
* Due: 09/22/2008
* ImagePanel.java - Manages ImagePanel Objects for the Airport Security Project
*/

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.IOException;
import java.net.URL;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel
{
	// Static Constants
	private static int MAXIMUM_CAPACITY = 30;
	private int WIDTH;
	private int HEIGHT;
	private static int spriteCounter = 30;
	private static int SPRITE_WIDTH = 60;
	private static int QUEUE_SIZE = 100;
	private int spriteCount;
	
	// Other Instance Data
	private int currentSprite;
	private int x;
	private int y;
	int i = 0;
	private URL urla;
	URL url;

	// Management of Background Image
	private static String backgroundImageFile = "detector.jpg";
	private static int BACKGROUND_IMAGE_HEIGHT = 380;
	
	// Management of Base Images
	private static String baseFile = "stantion.jpg";
	private static int BASE_WIDTH = 80;
	
	// Management of Sprite Paths, Names, and Queue Structures
	private static String SPRITE_PATH = "images\\";
	private CircularArrayQueue temporaryQueue, spriteTracker;
	private  ImageIcon[] spriteIcon;
	private static String[] sprites = {"arylonleft.gif", "arylonlefto.gif", "bbanileft.gif","bdragonleft.gif", "bmanileft.gif", "bwanileft.gif", "dragonleft.gif", 
		   "dwarfleft.gif", "fanileft.gif", "fatwomanleft.gif", "fatwomanlefto.gif", "kanileft.gif","knightleft.gif", "knightlefto.gif", "lefeinleft.gif", "manileft.gif", "manleft.gif", 
			   "manlefto.gif", "manleftp.gif", "mermaidleft.gif", "mohawkleft.gif", "mohawklefto.gif", 
			   "nanileft.gif", "oldguyleft2.gif", "oldguyleft2o.gif", "oldguyleft2p.gif", "pirateleft2.gif", 
			   "pirateleft2o.gif", "princessleft.gif", "rmanileft.gif", "robotleft.gif", "rwanileft.gif", 
			   "scholarleft.gif", "scholarlefto.gif", "tanileft.gif", "witchlefto.gif", "wmanileft.gif", 
			   "womanleft.gif", "womanlefto.gif", "womanleftp.gif", "wrobotleft.gif", "wwanileft.gif"};

	private ImageIcon background;
	private ImageIcon base;
	
	private Random rand;
	
	AirportSecurityGUI GUI;
	 
	public ImagePanel (int WINDOW_WIDTH, AirportSecurityGUI GUI)
	{
		//GUI Instances and Structures
	    this.GUI = GUI;
		spriteCount = MAXIMUM_CAPACITY+1;
		temporaryQueue = new CircularArrayQueue(QUEUE_SIZE);
		spriteTracker = new CircularArrayQueue(QUEUE_SIZE);

		//Height and Width
		WIDTH = WINDOW_WIDTH;
		HEIGHT = BACKGROUND_IMAGE_HEIGHT + BASE_WIDTH;	
		
		//Background and Sprite Icons
		base = new ImageIcon(baseFile);
		spriteIcon = new ImageIcon[spriteCounter];
		
		for(int i = 0; i < 30; i++)
		{
			spriteIcon[i]=new ImageIcon(SPRITE_PATH + sprites[i],sprites[i]);
		}
		
		// Random Seed
		rand = new Random();
		
		// Reset Layout Size Dimensions
		setPreferredSize(new Dimension(WIDTH, HEIGHT));	
	}
	
	public void paintComponent(Graphics page)
	{
		super.paintComponent(page);
			   	
		// Setup Icons and GUI Structure
		setBackground(Color.gray);
		x = 0;
		y = 0;
		background = new ImageIcon(backgroundImageFile);
		base = new ImageIcon(baseFile);
		background.paintIcon(this,page,x,y);
		spriteIcon = new ImageIcon[spriteCounter];
		for(i = 0; i < 30; i++)
		{
			// Generate URL/I for Path to Sprite
			urla = ReadSprites(SPRITE_PATH + sprites[i]);
			
			// Use generated URL/I to initilize Sprite
			spriteIcon[i] = new ImageIcon(urla, sprites[i]);
		}

		//Modify current paint height based on Background Image
		y = y + BACKGROUND_IMAGE_HEIGHT;
		
		//Use paintIcon function to generate icons on Panel
		for(x=0; x < WIDTH + BASE_WIDTH; x = x + BASE_WIDTH)
		{
			base.paintIcon(this, page, x, y);
		}
		
		// Use offset of y(90) and x(125) to place icons appropriately on station images.
		y -= BASE_WIDTH - 90;		
		x = 125;
		
		//Initialize the painting of the sprites on the SpritePanel
		while(!spriteTracker.isEmpty())
		{
			if (spriteTracker.first() == null)
				spriteIcon[0].paintIcon(this,page,x,y);
			else
				spriteIcon[(Integer)spriteTracker.first()].paintIcon(this,page,x,y);
			temporaryQueue.enQueue(spriteTracker.deQueue());
			x+=SPRITE_WIDTH;
		}

		while(!temporaryQueue.isEmpty())
		{
			spriteTracker.enQueue(temporaryQueue.deQueue());	
		}
	}

	public void addSprite()
	{
	   spriteTracker.enQueue(1 + rand.nextInt(spriteCounter - 1));		
	   repaint();
	}
	
	public void removeFrontSprite()
	{
		currentSprite = (Integer)spriteTracker.first();
		spriteTracker.deQueue();
		repaint();
	}
	
	public String getSprite()
	{
		return sprites[currentSprite] ;
	}

	public boolean isEmpty()
	{
		return spriteTracker.size() == 0;
	}
	
	public boolean isFull()
	{
		return spriteTracker.size() == spriteCount-1;
	}

	public int queueSize()
	{
		return spriteTracker.size();
	}	
	
	public int getCapacity()
	{
		return MAXIMUM_CAPACITY;
	}
	
	public URL ReadSprites(String filename)
	{
		try
		{
			url = new URL(GUI.getCodeBase(), filename);
			return url;
		}
		
		catch (java.net.MalformedURLException e)
		{
			System.out.println("Couldn't create image: badly specified URL");
			return null;
		}

		catch(IOException e)
		{
			System.out.println("IOException " + e.getMessage() );
		}
		return url;
	}
}